<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  install
 */

class m_install_actions_UserAction extends f_core_Action
{
	public function execute()
	{
        $formValues = $this->getFormValues('form');
        
        $this->addToModel('langs', m_backoffice_services_BackOfficeService::getAvailableBoLangs());

        if(!is_null($formValues))
        {
            $errors = $this->checkForm($formValues);

            if(count($errors) == 0)
            {
                $rightsService = m_rights_services_RightsService::getInstance();
                $rootNodeService = m_backoffice_services_RootNodeService::getInstance();
                $rights = $rightsService->getAllRights();

                // Add super admin group
                $rootNode = $rootNodeService->createModuleRootNode('users');
                $group = m_users_documents_Group::createNew()->setAuthor('pagizer')->setLabel('Admin')->save();
                $rightsService->saveRightsForGroup($group, $rights);
                $rootNode->addGroup($group)->save();

                // Generate crypt key
                $key = m_backoffice_services_BackOfficeService::getKey();

                $user = m_users_documents_User::createNew()->setLogin($formValues['login'])
                                                    ->setPassword(md5($formValues['password'].$key))
                                                    ->setEmail($formValues['email'])
                                                    ->setAuthor('pagizer')
                                                    ->setLabel($formValues['login'])
                                                    ->setFirstName($formValues['firstName'])
                                                    ->setLastName($formValues['lastName'])
                                                    ->setBackLang($formValues['lang'])
                                                    ->save();

                $group->addUser($user)->save();

                f_core_Controller::getInstance()->redirect(URL_RELATIVE.'/bo/install/Website');
            }
            else
            {
                $this->addToModel('errors', $errors);
                $this->addToModel('value', $formValues);
                $this->setView('input');
            }
        }
        else
        {
            $value['lang'] = f_core_Context::getInstance()->getBrowserLanguage();
            $this->addToModel('value', $value);
            $this->setView('input');
        }
	}

	public function validate()
	{
		if(!f_database_Provider::checkConnect())
		{
			return false;
		}
		else if (m_users_services_UserService::getInstance()->hasAdminUser())
		{
			return false;
		}

		return true;
	}

	public function errorHandler()
	{
		if(!f_database_Provider::checkConnect())
		{
		    $this->addToModel('error', 'db');
		}
		else
		{
            $this->addToModel('error', 'already');
		}

        $this->setView('error');
	}

    public function checkForm($formValues)
    {
        $errors = array();

        foreach($formValues as $fieldName => $fieldValue)
        {
            if($fieldValue == '')
            {
                $errors['required'] = true;
            }
        }

        if(!isset($errors['required']) && strlen($formValues['password']) < 5)
        {
            $errors['passwordLength'] = true;
        }

        if(!isset($errors['required']) && $formValues['password'] != $formValues['passwordConfirm'])
        {
            $errors['passwordConfirm'] = true;
        }



        return $errors;
    }
}